package zl.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zl.bean.User;
import zl.mapper.NovelMapper;
import zl.mapper.UserMapper;
import zl.service.UserService;

import java.sql.SQLException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NovelMapper novelMapper;
    @Override
    public int addUser(User user) {
        User u = userMapper.selectByUsername(user.getUsername(),user.getNickname());
        if(u==null){
            if(StrUtil.hasBlank(user.getImage())){
                user.setImage("default_user_image.png");
            }
            return userMapper.addUser(user);
        }else {
            return 3;
        }
    }

    @Override
    public int deleteUser(Integer id) throws SQLException {
        long count = novelMapper.selectNovelById(id);
        if(count==0){
            return userMapper.deleteUser(id);
        }else {
            return 3;
        }

    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public List<User> selectAllUser() {
        return userMapper.selectAllUser(null);
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public PageInfo<User> selectByPage(int currentPage, String keyword) throws SQLException {
        PageHelper.startPage(currentPage, 5);
        List<User> list =userMapper.selectAllUser(keyword);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        System.out.println("总记录数：" + pageInfo.getTotal());
        System.out.println("当前页：" + pageInfo.getPageNum());
        System.out.println("总页数：" + pageInfo.getPages());
        System.out.println("上一页：" + pageInfo.getPrePage());
        System.out.println("下一页：" + pageInfo.getNextPage());
        System.out.println("是否是首页：" + pageInfo.isIsFirstPage());
        System.out.println("是否是尾页：" + pageInfo.isIsLastPage());
        return pageInfo;
    }
}
